'use strict'

import { app, BrowserWindow } from 'electron'
const ipcMain = require('electron').ipcMain

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 690,
    width: 1020,
    useContentSize: true,
    frame: false,
    minWidth: 1020,
    minHeight: 690,
    webPreferences: {
      webSecurity: false,
      nodeIntegration: true
    }
  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
  mainWindow.on('maximize', () => {
    mainWindow.webContents.send('window-state', 'maximize');
  })
  mainWindow.on('unmaximize', () => {
    mainWindow.webContents.send('window-state', 'unmaximize');
  })


  ipcMain.on('window-state', (event, args) => {
    var cmd = args
    if (cmd === 'CloseWindow') {
      mainWindow.close()
    } else if (cmd === 'Maximizewindow') {
      if (mainWindow.isMaximized()) {
        mainWindow.restore()
      } else {
        mainWindow.maximize()
      }
    } else if (cmd === 'MinimizeWindow') {
      mainWindow.minimize()
    }
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
